﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFormsApp1.RelationForms
{
    public partial class FormRelationOneTable : Form
    {
        public List<TableWithKey> AllTables { get; internal set; }
        private DataTable m_selectedDataTable;
        public FormRelationOneTable()
        {
            InitializeComponent();
        }

        private void FormRelationOneTable_Load(object sender, EventArgs e)
        {
            for (var i = 0; i < AllTables.Count; i++)
            {
                this.comboBox1.Items.Add(AllTables[i].TableData.TableName);
            }
            this.comboBox1.SelectedIndex = 0;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            var table = AllTables[this.comboBox1.SelectedIndex];
            this.dataGridView1.DataSource = table.TableData.RefDataTable;
            m_selectedDataTable = table.TableData.RefDataTable;

            this.comboBox2.Items.Clear();
            this.comboBox4.Items.Clear();

            for (var i = 0; i < table.TableData.EntityColumns.Count; i++)
            {
                this.comboBox2.Items.Add(table.TableData.EntityColumns[i].ToString());
                this.comboBox4.Items.Add(table.TableData.EntityColumns[i].ToString());
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var str1 = this.comboBox2.Text;
            var str2 = this.comboBox4.Text;
            if (string.IsNullOrEmpty(str1)) { this.dataGridView2.DataSource = null; return; }
            if (string.IsNullOrEmpty(str2)) { this.dataGridView2.DataSource = null; return; }

            DataTable dataTable = new DataTable();
            dataTable.Columns.Add(str1);
            dataTable.Columns.Add(str2);

            for (var i = 0; i < m_selectedDataTable.Rows.Count; i++)
            {
                var dir = GetRowData(i);
                var strObj1 = FormRelationOneTableExporter.GetFormatEntityName(dir, str1);
                var strObj2 = FormRelationOneTableExporter.GetFormatEntityName(dir, str2);
                dataTable.Rows.Add(new object[] { strObj1, strObj2 });
            }

            this.dataGridView2.DataSource = dataTable;
        }
        private Dictionary<string, string> GetRowData(int rowIndex)
        {
            Dictionary<string, string> jeys = new Dictionary<string, string>();
            for (var i = 0; i < m_selectedDataTable.Columns.Count; i++)
            {
                jeys.Add(m_selectedDataTable.Columns[i].ColumnName, m_selectedDataTable.Rows[rowIndex][i].ToString());
            }
            return jeys;
        }
        

        private void 设为实体AToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.comboBox2.Text = "{" + m_selectedDataTable.Columns[m_lastSelectedColumIndex].ColumnName + "}";
            this.textBox2.Text = this.comboBox2.Text;
        }

        private void 设为实体BToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.comboBox4.Text = "{" + m_selectedDataTable.Columns[m_lastSelectedColumIndex].ColumnName + "}";
            this.textBox3.Text = this.comboBox4.Text;
        }
        private int m_lastSelectedColumIndex;
        private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            var columnName = e.ColumnIndex;
            m_lastSelectedColumIndex = columnName;

            if (e.Button == MouseButtons.Right)
            {
                var len = 0;
                for (var i = 0; i <= columnName; i++)
                {
                    len += dataGridView1.Columns[i].Width;
                }
                len -= dataGridView1.HorizontalScrollingOffset;
                this.contextMenuStrip1.Show(dataGridView1, len, 32);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "*.json|*.json";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                FormRelationOneTableExporter exporter = new FormRelationOneTableExporter()
                {
                    SaveFileName=dlg.FileName,
                    DataTableName=this.comboBox1.Text,
                    SourceDataTable= this.dataGridView1.DataSource as DataTable,
                    KeyIDDataTable = this.dataGridView2.DataSource as DataTable,
                    EntityAAliasName=this.textBox2.Text,
                    EntityBAliasName = this.textBox3.Text,
                    ConnectionName=this.textBox1.Text
                };
                exporter.SaveFile();
                MessageBox.Show("保存成功");
            }
        }
    }
}
